package com.xiaoge.demo.utils;


import com.xiaoge.demo.core.ServiceException;

import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * 作者：guoyzh
 * 时间：2018/11/20 16:21
 * 功能：参数校验
 */

public class ValidateUtils {
    private static ValidateUtils ourInstance;

    public static ValidateUtils getInstance() {
        if (ourInstance == null) {
            synchronized (ValidateUtils.class) {
                if (ourInstance == null) {
                    ourInstance = new ValidateUtils();
                }
            }
        }
        return ourInstance;
    }

    private ValidateUtils() {
    }

    /**
     * 对入参进行校验
     *
     * @param params
     */
    public void validateData(String... params) {
        if (null == params || params.length == 0) {
            throw new ServiceException("请求参数不合法");
        }
        for (String param : params) {
            if (StringUtils.isEmpty(param)) {
                throw new ServiceException("请求参数不能为空");
            }
        }
    }

    /**
     * 校验返回结果
     */
    public void checkResult(Object obj) {
        if (ObjectUtils.isEmpty(obj)) {
            throw new ServiceException("获取数据失败……");
        }
    }
}
